function f_check_disk_space_for_node()
{ #检查主库的磁盘空间剩余大小, 要求满足两倍于当前表大小
    declare mysql_local_free_size space_is_ok fun_name check mysql_local_free_size_per space_is_ok_per space_per _mysql_host
    fun_name="f_check_disk_space"
    _mysql_host="${1}"
    [ "${no_check_disk}x" == "falsex" ] && return 0
    for ((check=0;check<10;check++))
    do
        if [ "${global_mysql_node_disk_size["${_mysql_host}"]}x" == "x" ]
        then
            mysql_local_free_size="$(f_exe_ssh_comm "${_mysql_host}" "df"|grep -P "${mysql_data_mount_dir}$"|sort -nk 4|head -1)" || true
            global_mysql_node_disk_size["${_mysql_host}"]="${mysql_local_free_size}"
        else
            mysql_local_free_size=${global_mysql_node_disk_size["${_mysql_host}"]}
        fi

        if [ -z "${mysql_local_free_size}" ]
        then
            sleep 5 && continue
        fi

        #mysql_local_free_size="$(f_exe_ssh_comm "${_mysql_host}" "df"|grep -P "${mysql_data_mount_dir}$"|sort -nk 4|head -1)" || true
        mysql_local_free_size_per="${mysql_local_free_size}"
        #mysql_local_free_size="$(awk '{if ($4/1024>0) {printf( "%.f\n",$4/1024)} else{print 0}}' <<< "${mysql_local_free_size}")" || true
        mysql_local_free_size="$(awk '{if (($4-($2*0.1>314572800?314572800:$2*0.1))/1024>0) {printf( "%.f\n",($4-($2*0.1>314572800?314572800:$2*0.1))/1024)} else{print 0}}' <<< "${mysql_local_free_size}")" || true
        if [ -z "${mysql_local_free_size}" ] || [ "$(echo "${mysql_local_free_size}"|sed 's/[0-9.]//g')x" == "0x" ]
        then
            f_logging "$(eval echo ${log_addr}):ERROR" "f_exe_ssh_comm '${_mysql_host}' 'df' 获取'${_mysql_host}'机器剩余空间大小异常,请检查" "2" "1"
        fi

        #判断空间 要求剩余空间大于两倍表大小, disk_space_mark对应${dbadb_port} ${dbadb_db} dbadb_alter_table_repl_time 表的disk_space字段的值
        space_is_ok="$(awk -v r_size="${reserve_disk}" '{if ($1 > $2 * 2) {print 1}else if ($1 > $2 + r_size){print 2}else {print 0}}' <<< "${mysql_local_free_size} ${table_size}")" 
        space_is_ok_per="$(awk -v r_size="${reserve_disk}" '{if (((($3 + r_size + $7 * 1024 * 2)/$2))*100 > 90 && ($2 - ($3 + r_size + $7 * 1024 * 2) < 314572800)){print 2} else {print 1}}' <<< "${mysql_local_free_size_per} ${table_size}")" 
        space_per="$(awk -v r_size="${reserve_disk}" '{printf( "%.2f%, 当前使用率为 : %s\n",((($3 + r_size + $7 * 1024 * 2)/$2))*100,$5)}' <<< "${mysql_local_free_size_per} ${table_size}")" 
        f_logging "$(eval echo ${log_addr}):WARN" "获取到目标表大小${table_size}MB '${_mysql_host}' 剩余空间大小为 ${mysql_local_free_size} MB, 预计改表后的磁盘使用率为 : ${space_per} [${space_is_ok}:${space_is_ok_per}]" "execute:${execute}"

        if [ "${space_is_ok}x" == "2x" ] || [ "${space_is_ok_per}x" == "2x" ]
        then #space_is_ok=2表示不满足两倍
            if [ "${disk_space_mark}x" == "truex" ]
            then
                f_logging "$(eval echo ${log_addr}):WARN" "'${_mysql_host}' 机器剩余空间大小为'${mysql_local_free_size}MB', 不满足两倍表大小(或者磁盘空闲百分比小于10%且空闲空间小于300GB), 但是使用了'-S true'" "execute:${execute}"
            else
                if [ -n "${mysql_local_free_size}" ]
                then
                    return__="'${_mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'. 要求要大于两倍表大小(或者磁盘空闲百分比小于10%且空闲空间小于300GB), 不满足改表操作. "
                    return__="${return__} 如果确认当前主库机器剩余空间满足本次改表操作, 请联系dba处理(在管理机执行成功后让用户重新提交"
                    return__="${return__}  : main.sh -a disk_space -P ${mysql_port})"
                    return 1
                elif [ "${check}x" != "9x" ]
                then
                    sleep 1 && continue
                fi
            fi
        elif [ "${space_is_ok}x" == "0x" ] || [ "${space_is_ok_per}x" == "0x" ]
        then #space_is_ok=0表示不满足两倍且不满足【一倍+50G】
            if [ -n "${mysql_local_free_size}" ]
            then
                return__="获取到 '${_mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'. 要求要大于两倍表大小(或者磁盘空闲百分比小于10%且空闲空间小于300GB), 不满足改表操作. 请联系dba"
                return 1
            elif [ "${check}x" != "9x" ]
            then 
                sleep 5 && continue
            fi
        fi

        f_logging "$(eval echo ${log_addr}):REMAIND" "获取到 '${_mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'" "execute:${execute}"

        [ "${mysql_local_free_size}x" != "0x" ] && break || true
    done

    return 0

}

function f_check_disk_space()
{
    declare err_node

    err_node=""

    for _check_mysql_node in $(tr "," " " <<< "${mysql_host},${mysql_slave_info[@]}")
    do 
        if ! f_check_disk_space_for_node "${_check_mysql_node%:*}"
        then
            err_node="${_check_mysql_node},${err_node}"
        fi
    done

    if [ -n "${err_node}" ]
    then
        f_logging "$(eval echo ${log_addr}):ERROR" "获取到 '${err_node}' 机器剩余空间大小 '不满足改表, 请联系dba进行处理'" "execute:${execute}"
        return 1
    fi

    return 0

}

function f_check_disk_for_node()
{ #检查节点的磁盘空间, 如果使用率大于85%就清理binlog, 主要是避免改变产生大量binlog导致磁盘空间告警
    declare _disk_info _free_disk _free_disk_mark _use_per _use_per_mark _check_mysql_node _sec_binlog fun_name
    fun_name="f_check_disk_for_node"
    [ "${no_check_disk}x" == "falsex" ] && return 0
    [ -z "${print_check_disk_info}" ] && f_logging "$(eval echo ${log_addr}):REMAIND" "开始检查${mysql_host} $(sed -r 's/:[^,]+,/ /g' <<< "${mysql_slave_info[@]%:*}")磁盘空间使用率"
    print_check_disk_info=0 #无用变量, 仅用于打印上面这个提示信息
    for _check_mysql_node in $(tr "," " " <<< "${mysql_host},${mysql_slave_info[@]}")
    do 
        [ "${no_check_disk}x" == "flasex" -a "${_check_mysql_node}x" == "${mysql_host}x" ] && continue   #忽略检查磁盘空间
        _check_mysql_node="$(awk -F: '{print $1}' <<< "${_check_mysql_node}")"
        f_check_ssh_auth "${_check_mysql_node}" 2>/dev/null || continue
        while :
        do
            sleep 1
            [ ! -f "${osc_run_mark_file}" ] && break
            _disk_info="$(f_exe_ssh_comm "${_check_mysql_node}" "df"|grep -P "${mysql_data_mount_dir}$"|sort -nk 4|head -1)" || true

            _free_disk="$(awk -F' ' 'OFS=" ",NF-=2{print $NF}' <<< "${_disk_info}")" || true
            _free_disk_mark="$(awk -v num="${free_disk}" '{if ($1<num){print "error"}else if($1>0){print "ok"}}' <<< "${_free_disk}")" || true

            _use_per="$(awk -F' ' 'OFS=" ",NF-=1{print $NF}' <<< "${_disk_info}")" || true
            _use_per_mark="$(awk -v num="${disk_alarm}" -F'%' '{
                                                                   if ($1>=num+5){print "fault_error"} #磁盘使用率, 超过90%是严重错误
                                                                   else if($1>num+3){print "error"}    #磁盘使用率, 超过88%是错误
                                                                   else if($1>num){print "warn"}       #磁盘使用率, 超过85%是告警
                                                                   else{print "ok"}}' <<< "${_use_per}")" || true
            if [ -z "${_disk_info}" ] || [ -z "${_free_disk}" ] || [ -z "${_free_disk_mark}" ]
            then #任何值为空都是异常的
                f_logging "$(eval echo ${log_addr}):ERROR" "获取 '${_check_mysql_node}' 机器磁盘空间使用情况异常, 请检查" && break
            fi

            if [ "${_use_per_mark}x" == "fault_errorx" ]
            then
                f_logging "$(eval echo ${log_addr}):ERROR" "${_check_mysql_node}'机器磁盘使用率[${_use_per}]超过$((${disk_alarm}+5))%,现在终止改表操作,如有疑问请联系dba"
                f_stop_task_for_alter
            elif [ "${_use_per_mark}x" == "warnx" -a "${_free_disk_mark}x" == "errorx" ] || [ "${_use_per_mark}x" == "errorx" ]
            then #使用率大于85%且不足大小为300g, 或者使用率大于90%
                _sec_binlog="$(f_exe_sql_in_mysql "show binary logs" "${_check_mysql_node}" "${mysql_port}" "slave" "$(eval echo ${log_addr})"|head -2|tail -1|awk '{print $1}')"
                _count_binlog="$(f_exe_sql_in_mysql "show binary logs" "${_check_mysql_node}" "${mysql_port}" "slave" "$(eval echo ${log_addr})"|wc -l|awk '{if ($1>=50){print "ok"}}')"
                [ -z "${_sec_binlog}" ] && break || sleep 1   #binlog文件获取失败
                _fir_binlog="$(awk -F'.' '{printf ("%s.%06d\n",$1,$2-1)}' <<< "${_sec_binlog}")"
                if [ "${_count_binlog}x" == "okx" ]
                then #binlog文件个数要求大于50个
                    f_logging "$(eval echo ${log_addr}):ERROR" "'${_check_mysql_node}' 磁盘使用率[${_use_per}]超过${disk_alarm}%, 现在开始清理binlog[${_fir_binlog}"
                    f_exe_sql_in_mysql "purge binary logs to '${_sec_binlog}';" "${_check_mysql_node}" "${mysql_port}" "slave" "$(eval echo ${log_addr})" && continue || break
                else
                    f_logging "$(eval echo ${log_addr}):ERROR" "'${_check_mysql_node}' 磁盘使用率[${_use_per}]超过${disk_alarm}%, 应该清理binlog, 但是binlog文件不够50个, 清理失败"
                    break
                fi
            fi
            break
        done
    done
    return 0
}

